<?php
/**
 *
 * Description:
 * Author: falcon
 * Date: 18/5/11
 * Time: 下午2:27
 *
 */
if(substr(PHP_SAPI_NAME(),0,3) !== 'cli'){
   exit('bad_request')
}

require_once __DIR__ . '/config.php';

//$accessToken = $_SESSION['fb_access_token'];
$accessToken = file_get_contents(__DIR__ . '/accesstoken.txt');

$today = date('Ymd');
$data_file = __DIR__ . '/data/' . $today . '.txt';
$log_file = __DIR__ . '/logs/'.$today.'.txt';

$old_data = __DIR__ . '/data/'.date('Ymd',strtotime('-3 days')). '.txt';
$old_log = __DIR__ . '/logs/'.date('Ymd',strtotime('-3 days')). '.txt';

if(file_exists($old_data)) {
   unlink ($old_data);
}
if(file_exists($old_log)) {
  unlink($old_log);
}

foreach ($sites_config as $site) {


    if( !is_file($data_file) ) {
        $yesterday_data_file = __DIR__ . '/data/'.date('Ymd',strtotime('-1 day')). '.txt';
        if(file_exists( $yesterday_data_file )) {

            file_put_contents($data_file,file_get_contents( $yesterday_data_file ));
            file_put_contents($data_file,"\n=======\n",FILE_APPEND);
        }
    }

    $data_urls = !is_file($data_file) ? '' : file_get_contents( $data_file );

    $input = fb_file_get_contents($site . '/feed');



    if (preg_match_all('#<link>(.*?)</link>#', $input, $matches)) {


        foreach ($matches[1] as $link) {
            $link = str_replace(array("\r",'\n'),'',$link);
            if(strpos($data_urls,$link) !== false) {
                echo $log = sprintf("Cron [%s] : ignore   %s\n", date("Y-m-d H:i:s"), $link);
                file_put_contents($log_file, $log."\n", FILE_APPEND);
                continue;
            }

            $error = false;
            try {
                echo "pushing:".$link;
                // Get the \Facebook\GraphNodes\GraphUser object for the current user.
                // If you provided a 'default_access_token', the '{access-token}' is optional.
                $response = $fb->post('/?scrape=true&id=' . urlencode($link), [], $accessToken);
            } catch (\Facebook\Exceptions\FacebookResponseException $e) {
                // When Graph returns an error

                $error = 'Graph returned an error: ' . $e->getMessage();
                //exit;
            } catch (\Facebook\Exceptions\FacebookSDKException $e) {
                // When validation fails or other local issues
                $error = 'Facebook SDK returned an error: ' . $e->getMessage();
                //exit;
            }

            echo $log = sprintf("Cron: [%s] %s : %s\n", date("Y-m-d H:i:s"), $link, ($error!==false) ? $error : 'OK');

            file_put_contents($log_file, $log."\n", FILE_APPEND);

            if (!$error) {
                file_put_contents($data_file, $link."\n", FILE_APPEND);
            }


        }

    }

}


function fb_file_get_contents($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec ($ch);

    curl_close ($ch);

    return $server_output;
}
